// utils/validate.ts

// 身份证验证
export const validateIdCard = (idCard: string): boolean => {
  // 18位身份证正则
  const reg =
    /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
  if (!reg.test(idCard)) return false;

  // 验证校验码
  const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  const codes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  const idCardArray = idCard.split('');
  let sum = 0;

  for (let i = 0; i < 17; i += 1) {
    sum += parseInt(idCardArray[i], 10) * weights[i];
  }

  const lastCode = codes[sum % 11];
  return lastCode === idCardArray[17].toUpperCase();
};

// 护照验证
export const validatePassport = (passport: string): boolean => {
  // 护照号码一般为8-9位，可能是字母和数字的组合
  const reg = /^[a-zA-Z0-9]{8,9}$/;
  return reg.test(passport);
};

// 军官证验证
export const validateMilitaryOfficer = (officerCard: string): boolean => {
  // 军官证格式：字+数字，如"军字第1234567号"
  const reg = /^[\u4e00-\u9fa5]字第\d{7,8}号$/;
  return reg.test(officerCard);
};

// 通用证件验证
export const validateCertificate = (type: string, value: string): boolean => {
  switch (type) {
    case '1':
      return validateIdCard(value);
    case '2':
      return validatePassport(value);
    case '3':
      return validateMilitaryOfficer(value);
    default:
      return false;
  }
};

// Vue组件中使用示例
